3 * Base class for all changes lists.
5 * The class is used for formatting recent changes, related changes and watchlist.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
25 class ChangesList
extends ContextSource
{
31 protected $watchlist = false;
35 protected $rcCacheIndex;
36 protected $rclistOpen;
37 protected $rcMoveIndex;
40 protected $watchMsgCache;
43 * Changeslist constructor
45 * @param Skin|IContextSource $obj
47 public function __construct( $obj ) {
48 if ( $obj instanceof IContextSource
) {
49 $this->setContext( $obj );
50 $this->skin
= $obj->getSkin();
52 $this->setContext( $obj->getContext() );
55 $this->preCacheMessages();
56 $this->watchMsgCache
= new HashBagOStuff( [ 'maxKeys' => 50 ] );
60 * Fetch an appropriate changes list class for the specified context
61 * Some users might want to use an enhanced list format, for instance
63 * @param IContextSource $context
66 public static function newFromContext( IContextSource
$context ) {
67 $user = $context->getUser();
68 $sk = $context->getSkin();
70 if ( Hooks
::run( 'FetchChangesList', [ $user, &$sk, &$list ] ) ) {
71 $new = $context->getRequest()->getBool( 'enhanced', $user->getOption( 'usenewrc' ) );
73 return $new ?
new EnhancedChangesList( $context ) : new OldChangesList( $context );
84 * @param RecentChange $rc Passed by reference
85 * @param bool $watched (default false)
86 * @param int $linenumber (default null)
90 public function recentChangesLine( &$rc, $watched = false, $linenumber = null ) {
91 throw new RuntimeException( 'recentChangesLine should be implemented' );
95 * Sets the list to use a "<li class='watchlist-(namespace)-(page)'>" tag
98 public function setWatchlistDivs( $value = true ) {
99 $this->watchlist
= $value;
103 * @return bool True when setWatchlistDivs has been called
106 public function isWatchlist() {
107 return (bool)$this->watchlist
;
111 * As we use the same small set of messages in various methods and that
112 * they are called often, we call them once and save them in $this->message
114 private function preCacheMessages() {
115 if ( !isset( $this->message
) ) {
117 'cur', 'diff', 'hist', 'enhancedrc-history', 'last', 'blocklink', 'history',
118 'semicolon-separator', 'pipe-separator' ] as $msg
120 $this->message
[$msg] = $this->msg( $msg )->escaped();
126 * Returns the appropriate flags for new page, minor change and patrolling
127 * @param array $flags Associative array of 'flag' => Bool
128 * @param string $nothing To use for empty space
131 public function recentChangesFlags( $flags, $nothing = ' ' ) {
133 foreach ( array_keys( $this->getConfig()->get( 'RecentChangesFlags' ) ) as $flag ) {
134 $f .= isset( $flags[$flag] ) && $flags[$flag]
135 ? self
::flag( $flag )
143 * Get an array of default HTML class attributes for the change.
145 * @param RecentChange|RCCacheEntry $rc
146 * @param string|bool $watched Optionally timestamp for adding watched class
148 * @return array of classes
150 protected function getHTMLClasses( $rc, $watched ) {
152 $logType = $rc->mAttribs
['rc_log_type'];
155 $classes[] = Sanitizer
::escapeClass( 'mw-changeslist-log-' . $logType );
157 $classes[] = Sanitizer
::escapeClass( 'mw-changeslist-ns' .
158 $rc->mAttribs
['rc_namespace'] . '-' . $rc->mAttribs
['rc_title'] );
161 // Indicate watched status on the line to allow for more
162 // comprehensive styling.
163 $classes[] = $watched && $rc->mAttribs
['rc_timestamp'] >= $watched
164 ?
'mw-changeslist-line-watched'
165 : 'mw-changeslist-line-not-watched';
171 * Provide the "<abbr>" element appropriate to a given abbreviated flag,
172 * namely the flag indicating a new page, a minor edit, a bot edit, or an
173 * unpatrolled edit. By default in English it will contain "N", "m", "b",
174 * "!" respectively, plus it will have an appropriate title and class.
176 * @param string $flag One key of $wgRecentChangesFlags
177 * @return string Raw HTML
179 public static function flag( $flag ) {
180 static $flagInfos = null;
181 if ( is_null( $flagInfos ) ) {
182 global $wgRecentChangesFlags;
184 foreach ( $wgRecentChangesFlags as $key => $value ) {
185 $flagInfos[$key]['letter'] = wfMessage( $value['letter'] )->escaped();
186 $flagInfos[$key]['title'] = wfMessage( $value['title'] )->escaped();
187 // Allow customized class name, fall back to flag name
188 $flagInfos[$key]['class'] = Sanitizer
::escapeClass(
189 isset( $value['class'] ) ?
$value['class'] : $key );
193 // Inconsistent naming, bleh, kepted for b/c
195 'minoredit' => 'minor',
198 if ( isset( $map[$flag] ) ) {
202 return "<abbr class='" . $flagInfos[$flag]['class'] . "' title='" .
203 $flagInfos[$flag]['title'] . "'>" . $flagInfos[$flag]['letter'] .
208 * Returns text for the start of the tabular part of RC
211 public function beginRecentChangesList() {
212 $this->rc_cache
= [];
213 $this->rcMoveIndex
= 0;
214 $this->rcCacheIndex
= 0;
215 $this->lastdate
= '';
216 $this->rclistOpen
= false;
217 $this->getOutput()->addModuleStyles( 'mediawiki.special.changeslist' );
219 return '<div class="mw-changeslist">';
223 * @param ResultWrapper|array $rows
225 public function initChangesListRows( $rows ) {
226 Hooks
::run( 'ChangesListInitRows', [ $this, $rows ] );
230 * Show formatted char difference
231 * @param int $old Number of bytes
232 * @param int $new Number of bytes
233 * @param IContextSource $context
236 public static function showCharacterDifference( $old, $new, IContextSource
$context = null ) {
238 $context = RequestContext
::getMain();
243 $szdiff = $new - $old;
245 $lang = $context->getLanguage();
246 $config = $context->getConfig();
247 $code = $lang->getCode();
248 static $fastCharDiff = [];
249 if ( !isset( $fastCharDiff[$code] ) ) {
250 $fastCharDiff[$code] = $config->get( 'MiserMode' )
251 ||
$context->msg( 'rc-change-size' )->plain() === '$1';
254 $formattedSize = $lang->formatNum( $szdiff );
256 if ( !$fastCharDiff[$code] ) {
257 $formattedSize = $context->msg( 'rc-change-size', $formattedSize )->text();
260 if ( abs( $szdiff ) > abs( $config->get( 'RCChangedSizeThreshold' ) ) ) {
266 if ( $szdiff === 0 ) {
267 $formattedSizeClass = 'mw-plusminus-null';
268 } elseif ( $szdiff > 0 ) {
269 $formattedSize = '+' . $formattedSize;
270 $formattedSizeClass = 'mw-plusminus-pos';
272 $formattedSizeClass = 'mw-plusminus-neg';
275 $formattedTotalSize = $context->msg( 'rc-change-size-new' )->numParams( $new )->text();
277 return Html
::element( $tag,
278 [ 'dir' => 'ltr', 'class' => $formattedSizeClass, 'title' => $formattedTotalSize ],
279 $context->msg( 'parentheses', $formattedSize )->plain() ) . $lang->getDirMark();
283 * Format the character difference of one or several changes.
285 * @param RecentChange $old
286 * @param RecentChange $new Last change to use, if not provided, $old will be used
287 * @return string HTML fragment
289 public function formatCharacterDifference( RecentChange
$old, RecentChange
$new = null ) {
290 $oldlen = $old->mAttribs
['rc_old_len'];
293 $newlen = $new->mAttribs
['rc_new_len'];
295 $newlen = $old->mAttribs
['rc_new_len'];
298 if ( $oldlen === null ||
$newlen === null ) {
302 return self
::showCharacterDifference( $oldlen, $newlen, $this->getContext() );
306 * Returns text for the end of RC
309 public function endRecentChangesList() {
310 $out = $this->rclistOpen ?
"</ul>\n" : '';
317 * @param string $s HTML to update
318 * @param mixed $rc_timestamp
320 public function insertDateHeader( &$s, $rc_timestamp ) {
321 # Make date header if necessary
322 $date = $this->getLanguage()->userDate( $rc_timestamp, $this->getUser() );
323 if ( $date != $this->lastdate
) {
324 if ( $this->lastdate
!= '' ) {
327 $s .= Xml
::element( 'h4', null, $date ) . "\n<ul class=\"special\">";
328 $this->lastdate
= $date;
329 $this->rclistOpen
= true;
334 * @param string $s HTML to update
335 * @param Title $title
336 * @param string $logtype
338 public function insertLog( &$s, $title, $logtype ) {
339 $page = new LogPage( $logtype );
340 $logname = $page->getName()->setContext( $this->getContext() )->escaped();
341 $s .= $this->msg( 'parentheses' )->rawParams( Linker
::linkKnown( $title, $logname ) )->escaped();
345 * @param string $s HTML to update
346 * @param RecentChange $rc
347 * @param bool $unpatrolled
349 public function insertDiffHist( &$s, &$rc, $unpatrolled ) {
352 $rc->mAttribs
['rc_type'] == RC_NEW ||
353 $rc->mAttribs
['rc_type'] == RC_LOG ||
354 $rc->mAttribs
['rc_type'] == RC_CATEGORIZE
356 $diffLink = $this->message
['diff'];
357 } elseif ( !self
::userCan( $rc, Revision
::DELETED_TEXT
, $this->getUser() ) ) {
358 $diffLink = $this->message
['diff'];
361 'curid' => $rc->mAttribs
['rc_cur_id'],
362 'diff' => $rc->mAttribs
['rc_this_oldid'],
363 'oldid' => $rc->mAttribs
['rc_last_oldid']
366 $diffLink = Linker
::linkKnown(
368 $this->message
['diff'],
369 [ 'tabindex' => $rc->counter
],
373 if ( $rc->mAttribs
['rc_type'] == RC_CATEGORIZE
) {
374 $diffhist = $diffLink . $this->message
['pipe-separator'] . $this->message
['hist'];
376 $diffhist = $diffLink . $this->message
['pipe-separator'];
378 $diffhist .= Linker
::linkKnown(
380 $this->message
['hist'],
383 'curid' => $rc->mAttribs
['rc_cur_id'],
384 'action' => 'history'
389 // @todo FIXME: Hard coded ". .". Is there a message for this? Should there be?
390 $s .= $this->msg( 'parentheses' )->rawParams( $diffhist )->escaped() .
391 ' <span class="mw-changeslist-separator">. .</span> ';
395 * @param string $s Article link will be appended to this string, in place.
396 * @param RecentChange $rc
397 * @param bool $unpatrolled
398 * @param bool $watched
399 * @deprecated since 1.27, use getArticleLink instead.
401 public function insertArticleLink( &$s, RecentChange
$rc, $unpatrolled, $watched ) {
402 $s .= $this->getArticleLink( $rc, $unpatrolled, $watched );
406 * @param RecentChange $rc
407 * @param bool $unpatrolled
408 * @param bool $watched
409 * @return string HTML
412 public function getArticleLink( &$rc, $unpatrolled, $watched ) {
414 if ( $rc->getTitle()->isRedirect() ) {
415 $params = [ 'redirect' => 'no' ];
418 $articlelink = Linker
::link(
421 [ 'class' => 'mw-changeslist-title' ],
424 if ( $this->isDeleted( $rc, Revision
::DELETED_TEXT
) ) {
425 $articlelink = '<span class="history-deleted">' . $articlelink . '</span>';
427 # To allow for boldening pages watched by this user
428 $articlelink = "<span class=\"mw-title\">{$articlelink}</span>";
430 $articlelink .= $this->getLanguage()->getDirMark();
432 # TODO: Deprecate the $s argument, it seems happily unused.
434 Hooks
::run( 'ChangesListInsertArticleLink',
435 [ &$this, &$articlelink, &$s, &$rc, $unpatrolled, $watched ] );
437 return "{$s} {$articlelink}";
441 * Get the timestamp from $rc formatted with current user's settings
444 * @param RecentChange $rc
445 * @return string HTML fragment
447 public function getTimestamp( $rc ) {
448 // @todo FIXME: Hard coded ". .". Is there a message for this? Should there be?
449 return $this->message
['semicolon-separator'] . '<span class="mw-changeslist-date">' .
450 $this->getLanguage()->userTime(
451 $rc->mAttribs
['rc_timestamp'],
453 ) . '</span> <span class="mw-changeslist-separator">. .</span> ';
457 * Insert time timestamp string from $rc into $s
459 * @param string $s HTML to update
460 * @param RecentChange $rc
462 public function insertTimestamp( &$s, $rc ) {
463 $s .= $this->getTimestamp( $rc );
467 * Insert links to user page, user talk page and eventually a blocking link
469 * @param string &$s HTML to update
470 * @param RecentChange &$rc
472 public function insertUserRelatedLinks( &$s, &$rc ) {
473 if ( $this->isDeleted( $rc, Revision
::DELETED_USER
) ) {
474 $s .= ' <span class="history-deleted">' .
475 $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
477 $s .= $this->getLanguage()->getDirMark() . Linker
::userLink( $rc->mAttribs
['rc_user'],
478 $rc->mAttribs
['rc_user_text'] );
479 $s .= Linker
::userToolLinks( $rc->mAttribs
['rc_user'], $rc->mAttribs
['rc_user_text'] );
484 * Insert a formatted action
486 * @param RecentChange $rc
489 public function insertLogEntry( $rc ) {
490 $formatter = LogFormatter
::newFromRow( $rc->mAttribs
);
491 $formatter->setContext( $this->getContext() );
492 $formatter->setShowUserToolLinks( true );
493 $mark = $this->getLanguage()->getDirMark();
495 return $formatter->getActionText() . " $mark" . $formatter->getComment();
499 * Insert a formatted comment
500 * @param RecentChange $rc
503 public function insertComment( $rc ) {
504 if ( $this->isDeleted( $rc, Revision
::DELETED_COMMENT
) ) {
505 return ' <span class="history-deleted">' .
506 $this->msg( 'rev-deleted-comment' )->escaped() . '</span>';
508 return Linker
::commentBlock( $rc->mAttribs
['rc_comment'], $rc->getTitle() );
513 * Returns the string which indicates the number of watching users
514 * @param int $count Number of user watching a page
517 protected function numberofWatchingusers( $count ) {
521 $cache = $this->watchMsgCache
;
522 return $cache->getWithSetCallback( $count, $cache::TTL_INDEFINITE
,
523 function () use ( $count ) {
524 return $this->msg( 'number_of_watching_users_RCview' )
525 ->numParams( $count )->escaped();
531 * Determine if said field of a revision is hidden
532 * @param RCCacheEntry|RecentChange $rc
533 * @param int $field One of DELETED_* bitfield constants
536 public static function isDeleted( $rc, $field ) {
537 return ( $rc->mAttribs
['rc_deleted'] & $field ) == $field;
541 * Determine if the current user is allowed to view a particular
542 * field of this revision, if it's marked as deleted.
543 * @param RCCacheEntry|RecentChange $rc
545 * @param User $user User object to check, or null to use $wgUser
548 public static function userCan( $rc, $field, User
$user = null ) {
549 if ( $rc->mAttribs
['rc_type'] == RC_LOG
) {
550 return LogEventsList
::userCanBitfield( $rc->mAttribs
['rc_deleted'], $field, $user );
552 return Revision
::userCanBitfield( $rc->mAttribs
['rc_deleted'], $field, $user );
557 * @param string $link
558 * @param bool $watched
561 protected function maybeWatchedLink( $link, $watched = false ) {
563 return '<strong class="mw-watched">' . $link . '</strong>';
565 return '<span class="mw-rc-unwatched">' . $link . '</span>';
569 /** Inserts a rollback link
572 * @param RecentChange $rc
574 public function insertRollback( &$s, &$rc ) {
575 if ( $rc->mAttribs
['rc_type'] == RC_EDIT
576 && $rc->mAttribs
['rc_this_oldid']
577 && $rc->mAttribs
['rc_cur_id']
579 $page = $rc->getTitle();
580 /** Check for rollback and edit permissions, disallow special pages, and only
581 * show a link on the top-most revision */
582 if ( $this->getUser()->isAllowed( 'rollback' )
583 && $rc->mAttribs
['page_latest'] == $rc->mAttribs
['rc_this_oldid']
585 $rev = new Revision( [
587 'id' => $rc->mAttribs
['rc_this_oldid'],
588 'user' => $rc->mAttribs
['rc_user'],
589 'user_text' => $rc->mAttribs
['rc_user_text'],
590 'deleted' => $rc->mAttribs
['rc_deleted']
592 $s .= ' ' . Linker
::generateRollback( $rev, $this->getContext() );
598 * @param RecentChange $rc
602 public function getRollback( RecentChange
$rc ) {
604 $this->insertRollback( $s, $rc );
610 * @param RecentChange $rc
611 * @param array $classes
613 public function insertTags( &$s, &$rc, &$classes ) {
614 if ( empty( $rc->mAttribs
['ts_tags'] ) ) {
618 list( $tagSummary, $newClasses ) = ChangeTags
::formatSummaryRow(
619 $rc->mAttribs
['ts_tags'],
623 $classes = array_merge( $classes, $newClasses );
624 $s .= ' ' . $tagSummary;
628 * @param RecentChange $rc
629 * @param array $classes
633 public function getTags( RecentChange
$rc, array &$classes ) {
635 $this->insertTags( $s, $rc, $classes );
639 public function insertExtra( &$s, &$rc, &$classes ) {
640 // Empty, used for subclasses to add anything special.
643 protected function showAsUnpatrolled( RecentChange
$rc ) {
644 return self
::isUnpatrolled( $rc, $this->getUser() );
648 * @param object|RecentChange $rc Database row from recentchanges or a RecentChange object
652 public static function isUnpatrolled( $rc, User
$user ) {
653 if ( $rc instanceof RecentChange
) {
654 $isPatrolled = $rc->mAttribs
['rc_patrolled'];
655 $rcType = $rc->mAttribs
['rc_type'];
656 $rcLogType = $rc->mAttribs
['rc_log_type'];
658 $isPatrolled = $rc->rc_patrolled
;
659 $rcType = $rc->rc_type
;
660 $rcLogType = $rc->rc_log_type
;
663 if ( !$isPatrolled ) {
664 if ( $user->useRCPatrol() ) {
667 if ( $user->useNPPatrol() && $rcType == RC_NEW
) {
670 if ( $user->useFilePatrol() && $rcLogType == 'upload' ) {
679 * Determines whether a revision is linked to this change; this may not be the case
680 * when the categorization wasn't done by an edit but a conditional parser function
684 * @param RecentChange|RCCacheEntry $rcObj
687 protected function isCategorizationWithoutRevision( $rcObj ) {
688 return intval( $rcObj->getAttribute( 'rc_type' ) ) === RC_CATEGORIZE
689 && intval( $rcObj->getAttribute( 'rc_this_oldid' ) ) === 0;